﻿if (!ie) var ie = (window.navigator.userAgent.indexOf ("MSIE") >= 0);
var Ajax = function () {
    var request = ie ? new ActiveXObject ("Microsoft.XMLHTTP") : new XMLHttpRequest ();
    var requestMethod = "POST";
    var requestProcessor = null;
    var cleanup = null;

    var loaded = function () {
    };
    var error = null;

    this.setMethod = function (method) { requestMethod = method; }
    this.setProcessor = function (processor) { requestProcessor = processor; }
    this.setOnLoadedListener = function (listener) { loaded = listener; }
    this.setOnErrorListener = function (listener) { error = listener; }
    this.setOnCleanUpListener = function (listener) { cleanup = listener; }

    function processResult (request) {
        if (request.status == 404) {
            alert ("找不到服务：" + requestProcessor);
            return;
        }
        if (request.status == 200) {
            var doc = null;
            doc = DOMDelegate.createDocument ();
            doc.loadXML (request.responseText);

            var root = doc.firstChild;
            while (root && root.nodeType != 1) root = root.nextSibling;
            var node = root.firstChild;

            var statusCode;
            var content;
            while (node) {
                if (node.nodeName == "status-code") statusCode = DOMDelegate.getNodeValue (node);
                else if (node.nodeName == "body") {
                    content = node.firstChild;
                    while (content.nodeType != 1) content = content.nextSibling;
                }
                node = node.nextSibling;
            }
            if (statusCode == 200) {
				loaded (content);
            } else {
                if (error) {
                    var message;
                    node = content.firstChild;
                    while (node) {
                        if (node.nodeName == "message") message = node;
                        node = node.nextSibling;
                    }
                    error (DOMDelegate.getNodeValue (message));
                }
                else
                {
                    var message, stack;
                    node = content.firstChild;
                    while (node) {
                        if (node.nodeName == "message") message = node;
                        else if (node.nodeName == "stack") stack = node;
                        node = node.nextSibling;
                    }

                    var text = "Error   : " + statusCode + "\n";
                    text    += "Message : " + DOMDelegate.getNodeValue (message) + "\n\n";
                    text    += "Would you like to view detail? ";

                    if (confirm (text)) {
                        alert (DOMDelegate.getNodeValue (stack));
                    }
                }
            }        
        } else {
            alert("服务器内部错误");
        }
    }

    this.send = function (parameters) {
		var remoteService = requestProcessor;
		if (requestMethod.toUpperCase() == "GET") remoteService += "?" + parameters;
        request.open (requestMethod, remoteService);
        if (requestMethod.toUpperCase() == "POST")
            request.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
        request.onreadystatechange = function () {
            if (request.readyState == 4) {
                try {
                    processResult (request);
                } catch (E) {
					Logger.debug (E);
                } finally {
                    if (cleanup) cleanup ();
                }
            }
        }
        request.send (requestMethod.toUpperCase() == "GET" ? null : parameters);
    }
}